﻿package mortal.game.view.hardware
{
    import com.gengine.core.frame.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.gengine.manager.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;
    import mortal.component.window.*;
    import mortal.game.manager.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.alertwins.*;
    import mortal.mvc.interfaces.*;

    public class HardwareTipWindow extends SmallWindow
    {
        private var _btnClose:GLoadingButton;
        private var _isAddJs:Boolean = false;
        private var _hardWareLoader:Loader;
        private var mc:MovieClip;
        private static var _instance:HardwareTipWindow;

        public function HardwareTipWindow(param1:ILayer = null)
        {
            super(param1);
            setSize(524, 365);
            title = "硬件加速";
            return;
        }// end function

        override protected function configParams() : void
        {
            super.configParams();
            paddingBottom = 70;
            return;
        }// end function

        override protected function setWindowCenter() : void
        {
            return;
        }// end function

        override protected function addCloseButton() : void
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.DiverTip, 8, 34, this));
            this._btnClose = UIFactory.gLoadingButton(ImagesConst.GotoCheck_upSkin, 222, 320, 108, 44, this);
            this._btnClose.configEventListener(MouseEvent.CLICK, this.onClickClose);
            return;
        }// end function

        private function onClickClose(event:MouseEvent) : void
        {
            this.hide();
            this.showSettingPanel();
            return;
        }// end function

        private function showSettingPanel() : void
        {
            Security.showSettings(SecurityPanel.DISPLAY);
            MsgManager.showTaskTarget("亲，勾选硬件加速，就可以愉快的玩耍了");
            Global.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onActivate);
            return;
        }// end function

        private function setHardwareText(param1:String) : void
        {
            var _loc_2:* = param1.indexOf("userDisabled") != -1;
            if (_loc_2)
            {
                this.showSettingPanel();
            }
            else
            {
                Alert.showCloseBtn = false;
                Alert.okLabel = "立即刷新";
                Alert.buttonWidth = 80;
                Alert.alertWinRenderer = TimerAlertWin;
                Alert.timerOut = 10;
                Alert.show("已加速，需要<font color=\'#1ccc31\'>刷新</font>后立即生效！", null, Alert.OK, null, this.onCloseAlert);
            }
            return;
        }// end function

        private function onCloseAlert(param1:int) : void
        {
            BrowerManager.instance.reload();
            return;
        }// end function

        private function complete(event:Event) : void
        {
            return;
        }// end function

        private function onTimer(param1:SecTimer) : void
        {
            this.testHardWare();
            return;
        }// end function

        private function onActivate(event:Event) : void
        {
            setTimeout(this.checkHardWare, 5000);
            Global.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.onActivate);
            return;
        }// end function

        private function checkHardWare() : void
        {
            try
            {
                if (ExternalInterface.available)
                {
                    ExternalInterface.call("startHardWareTest");
                }
                if (!this._isAddJs)
                {
                    this._isAddJs = true;
                    if (ExternalInterface.available)
                    {
                        ExternalInterface.addCallback("setHardwareText", this.setHardwareText);
                    }
                }
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        private function reShowPanel() : void
        {
            HardwareTipWindow.instance.show();
            return;
        }// end function

        public function testHardWare() : void
        {
            this._hardWareLoader = new Loader();
            this._hardWareLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoadHardWareCompleteHandler);
            this._hardWareLoader.load(new URLRequest("TestHardwareIsOpen.swf?i=" + Math.random()));
            return;
        }// end function

        private function onLoadHardWareCompleteHandler(event:Event) : void
        {
            var _loc_2:* = event.target as LoaderInfo;
            _loc_2.removeEventListener(Event.COMPLETE, this.onLoadHardWareCompleteHandler);
            this.mc = _loc_2.content as MovieClip;
            this.mc.responseFun = this.isOpenHardWare;
            this.addChild(this.mc);
            return;
        }// end function

        private function isOpenHardWare(param1:Boolean) : void
        {
            this.removeChild(this.mc);
            Log.debug(Language.getString(11706), param1);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._btnClose.dispose(param1);
            this._btnClose = null;
            return;
        }// end function

        public static function get instance() : HardwareTipWindow
        {
            if (!_instance)
            {
                _instance = new HardwareTipWindow;
            }
            return _instance;
        }// end function

    }
}
